import 'package:flutter/material.dart';
import 'package:flutter_cwhisky/common/style/icons.dart';
import 'package:flutter_cwhisky/component/image_view.dart';
import 'package:flutter_cwhisky/component/text_view.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

import '../../../bean/mall_home_entity.dart';
import '../../../common/style/colors.dart';
import '../../../common/style/dimens.dart';
import '../../../common/style/style.dart';
import '../mall_good.dart';

class MultiplePrefectureComponent extends StatelessWidget {
  final MallHomeDataPrefecture prefecture;

  const MultiplePrefectureComponent({Key? key, required this.prefecture}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      clipBehavior: Clip.antiAlias,
      margin: EdgeInsets.only(left: Dimens.horizontalMargin, right: Dimens.horizontalMargin, top: 12.r),
      decoration: BoxDecoration(
          color: AppColors.white, borderRadius: BorderRadius.circular(8.r), boxShadow: AppStyle.commonShadow),
      child: Column(
        children: [
          Container(
            height: 46.r,
            padding: EdgeInsets.only(left: 15.r, right: 15.r),
            decoration: const BoxDecoration(
                gradient: LinearGradient(
                    begin: Alignment.topCenter,
                    end: Alignment.bottomCenter,
                    colors: [Color(0x80F5E9DC), Color(0x00FFEFE6)])),
            child: Row(
              children: [
                Expanded(
                    child: TextView(
                  text: prefecture.name ?? '',
                  fontColor: AppColors.color332B29,
                  fontSize: 16.sp,
                  fontWeight: FontWeight.w500,
                )),
                Row(
                  children: [
                    TextView(
                      text: '更多',
                      fontColor: AppColors.color99,
                      fontSize: 11.sp,
                    ),
                    Icon(
                      AppIcons.icArrowRight,
                      size: 8.r,
                      color: AppColors.color99,
                    )
                  ],
                )
              ],
            ),
          ),
          Column(
            children: [_buildBrands(), _buildMoreBrands()],
          )
        ],
      ),
    );
  }

  _buildBrands() {
    if (prefecture.children!.length == 2) {
      return Container(
        height: 190.r,
        child: Row(
          children: [
            Flexible(
                child: ImageView(
              url: prefecture.children![0].image ?? '',
            )),
            Flexible(
                child: ImageView(
              url: prefecture.children![1].image ?? '',
            ))
          ],
        ),
      );
    } else {
      return Container(
        height: 190.r,
        child: Row(
          children: [
            Container(
              width: 150.r,
              child: ImageView(
                url: prefecture.children![0].image ?? '',
                height: 190.r,
                fit: BoxFit.fill,
              ),
            ),
            Expanded(
                child: Column(
              children: [
                Flexible(
                  child: ImageView(
                    url: prefecture.children![1].image ?? '',
                    fit: BoxFit.fill,
                  ),
                ),
                Flexible(
                  child: ImageView(
                    url: prefecture.children![2].image ?? '',
                    fit: BoxFit.fill,
                  ),
                ),
              ],
            ))
          ],
        ),
      );
    }
  }

  _buildMoreBrands() {
    return prefecture.children!.length >= 8
        ? Container(
            height: 66.r,
            padding: EdgeInsets.symmetric(vertical: 4.r),
            child: ListView.builder(
                physics: const BouncingScrollPhysics(),
                itemCount: prefecture.children!.length,
                scrollDirection: Axis.horizontal,
                itemBuilder: (context, index) {
                  return _buildBrandItem(prefecture.children![index], index);
                }),
          )
        : const SizedBox();
  }

  _buildBrandItem(MallHomeDataPrefectureChildren item, int index) {
    return Container(
      margin: EdgeInsets.only(left: index == 0 ? 8.r : 18.r),

      child: AspectRatio(
        aspectRatio: 1,
        child: ImageView(
          url: item.brandImage,
          width: 58.r,
          height: 58.r,
        ),
      ),
    );
  }
}
